/**
 * @Author: YangYun
 * @Date: 2020/8/25 13:37
 */
package fund

import (
	"git.900sui.cn/kc/base/common/toolLib"
	"git.900sui.cn/kc/kcgin/orm"
	"git.900sui.cn/kc/rpcPay/common/models"
	_const "git.900sui.cn/kc/rpcPay/lang/const"
	"git.900sui.cn/kc/rpcinterface/interface/pay"
	"strconv"
	"time"
)

type PlatformFundLogic struct {
}

func (p *PlatformFundLogic) BusPay(busId int, orderSn string, payChannel int, amount string, orm ...orm.Ormer) error {
	platformFundModel := new(models.PlatformFundModel).Init(orm...)
	platformFundLogModel := new(models.PlatformFundLogModel).Init(orm...)
	platformClearingModel := new(models.PlatformClearingModel).Init(orm...)
	billDate := time.Now().Add(time.Hour * 24).Format("20060102")
	clearingInfo := platformClearingModel.GetByBilldate(billDate)
	if len(clearingInfo) == 0{
		platformClearingModel.Insert(map[string]interface{}{
			platformClearingModel.Field.F_bill_date:billDate,
			platformClearingModel.Field.F_amount:amount,
			platformClearingModel.Field.F_agent_type:payChannel,
			platformClearingModel.Field.F_create_time:time.Now().Local().Unix(),
		})
	}else{
		clearingId, _ := strconv.Atoi(clearingInfo[platformClearingModel.Field.F_id].(string))
		platformClearingModel.UpdateById(clearingId, map[string]interface{}{
			platformClearingModel.Field.F_amount:[]interface{}{"inc", amount},
		})
	}

	rowLen, err := platformFundModel.UpdateByPayChannel(payChannel, map[string]interface{}{
		platformFundModel.Field.F_amount: []interface{}{"inc", amount},
	})
	if err != nil {
		return err
	}
	if rowLen != 1 {
		return toolLib.CreateKcErr(_const.PAY_ERROR)
	}
	insert := platformFundLogModel.Insert(map[string]interface{}{
		platformFundLogModel.Field.F_pay_channel:      payChannel,
		platformFundLogModel.Field.F_fund_source_id:   busId,
		platformFundLogModel.Field.F_fund_source_type: pay.FUND_SOURCE_TYPE_bus,
		platformFundLogModel.Field.F_amount:           amount,
		platformFundLogModel.Field.F_type:             pay.TYPE_charge_in,
		platformFundLogModel.Field.F_order_sn:         orderSn,
		platformFundLogModel.Field.F_order_type:       pay.ORDER_TYPE_pay,
		platformFundLogModel.Field.F_create_time:      time.Now().Unix(),
	})
	if insert <= 0 {
		return toolLib.CreateKcErr(_const.PAY_ERROR)
	}

	return nil
}

// 上层自己开启事务
func (p *PlatformFundLogic) InsureFundAmount(insureId int, orderSn string, payChannel int, amount string, orm ...orm.Ormer) error {
	platformFundModel := new(models.PlatformFundModel).Init(orm...)
	platformFundLogModel := new(models.PlatformFundLogModel).Init(orm...)

	rowLen, err := platformFundModel.UpdateByPayChannel(payChannel, map[string]interface{}{
		platformFundModel.Field.F_amount: []interface{}{"dec", amount},
	})
	if err != nil {
		return err
	}
	if rowLen != 1 {
		return toolLib.CreateKcErr(_const.PAY_ERROR)
	}
	insert := platformFundLogModel.Insert(map[string]interface{}{
		platformFundLogModel.Field.F_pay_channel:      payChannel,
		platformFundLogModel.Field.F_fund_source_id:   insureId,
		platformFundLogModel.Field.F_fund_source_type: pay.FUND_SOURCE_TYPE_insure,
		platformFundLogModel.Field.F_amount:           amount,
		platformFundLogModel.Field.F_type:             pay.TYPE_charge_out,
		platformFundLogModel.Field.F_order_sn:         orderSn,
		platformFundLogModel.Field.F_order_type:       pay.ORDER_TYPE_insure_fund_fee,
		platformFundLogModel.Field.F_create_time:      time.Now().Unix(),
	})
	if insert <= 0 {
		return toolLib.CreateKcErr(_const.PAY_ERROR)
	}
	return nil
}

// 上层自己开启事务
func (p *PlatformFundLogic) RenewInsureFundAmount(insureId int, orderSn string, payChannel int, amount string, orm ...orm.Ormer) error {
	platformFundModel := new(models.PlatformFundModel).Init(orm...)
	platformFundLogModel := new(models.PlatformFundLogModel).Init(orm...)

	rowLen, err := platformFundModel.UpdateByPayChannel(payChannel, map[string]interface{}{
		platformFundModel.Field.F_amount: []interface{}{"dec", amount},
	})
	if err != nil {
		return err
	}
	if rowLen != 1 {
		return toolLib.CreateKcErr(_const.PAY_ERROR)
	}
	insert := platformFundLogModel.Insert(map[string]interface{}{
		platformFundLogModel.Field.F_pay_channel:      payChannel,
		platformFundLogModel.Field.F_fund_source_id:   insureId,
		platformFundLogModel.Field.F_fund_source_type: pay.FUND_SOURCE_TYPE_renew_insure,
		platformFundLogModel.Field.F_amount:           amount,
		platformFundLogModel.Field.F_type:             pay.TYPE_charge_out,
		platformFundLogModel.Field.F_order_sn:         orderSn,
		platformFundLogModel.Field.F_order_type:       pay.ORDER_TYPE_renew_insure_fund_fee,
		platformFundLogModel.Field.F_create_time:      time.Now().Unix(),
	})
	if insert <= 0 {
		return toolLib.CreateKcErr(_const.PAY_ERROR)
	}
	return nil
}
//根据订单号获取代付状态
func (p *PlatformFundLogic) GetAgentInfoByOrderSn(orderSn *string)  (reply pay.ReplyGetAgentInfoByOrderSn, err error){
	mOrderRelation := new(models.BusClearingOrderRealationModel).Init()
	orderInfo := mOrderRelation.GetByOrderSn(*orderSn)
	if len(orderInfo) == 0{
		err = toolLib.CreateKcErr(_const.NOT_ORDER_RELATION)
		return
	}

	mBusClear := new(models.BusClearingModel).Init(mOrderRelation.Model.GetOrmer())
	clearId, _ := strconv.Atoi(orderInfo[mOrderRelation.Field.F_clear_id].(string))
	clearInfo := mBusClear.GetById(clearId)
	if len(clearInfo) == 0{
		err = toolLib.CreateKcErr(_const.NOT_AGENT_INFO)
		return
	}

	reply.BusClearId	= clearId
	reply.Status, _ = strconv.Atoi(clearInfo[mBusClear.Field.F_status].(string))
	reply.BusId,  _ = strconv.Atoi(clearInfo[mBusClear.Field.F_bus_id].(string))
	reply.AgentType, _ = strconv.Atoi(clearInfo[mBusClear.Field.F_agent_type].(string))

	return
}

//根据id 获取代付状态
func (p *PlatformFundLogic) GetAgentInfoByClearid(clearId *int) (reply pay.ReplyGetAgentInfoByOrderSn, err error) {
	mBusClear := new(models.BusClearingModel).Init()
	clearInfo := mBusClear.GetById(*clearId)
	if len(clearInfo) == 0{
		err = toolLib.CreateKcErr(_const.NOT_AGENT_INFO)
		return
	}

	reply.BusClearId	= *clearId
	reply.Status, _ 	= strconv.Atoi(clearInfo[mBusClear.Field.F_status].(string))
	reply.BusId,  _ 	= strconv.Atoi(clearInfo[mBusClear.Field.F_bus_id].(string))
	reply.AgentType, _ 	= strconv.Atoi(clearInfo[mBusClear.Field.F_agent_type].(string))
	return
}